package com.sxhuayuan.parking.compenent.web;

import org.springframework.util.Assert;

import com.alibaba.fastjson.JSON;
import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.utils.SpringUtils;

/**
 * 接口统一返回消息对象
 */
public class RespMessage {

	public enum Status {
		/** 请求成功 */
		OK(0),

		/** 输入校验失败 */
		VALID_ERR(1),

		/** 业务失败 */
		ERROR(2),

		/** 无效令牌 */
		INVALID_TOKEN(100),

		/** 没有权限 */
		NON_PERMISSION(101),

		/** 未知错误 */
		UNKNOWN_ERR(-1);

		private Integer status;

		public Integer getStatus() {
			return status;
		}

		public void setStatus(Integer status) {
			this.status = status;
		}

		private Status(Integer status) {
			this.status = status;
		}
	}

	private Integer status;

	private String err = "";

	private Object datas;

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getErr() {
		return err;
	}

	public void setErr(String err) {
		this.err = err;
	}

	public Object getDatas() {
		return datas;
	}

	public void setDatas(Object datas) {
		this.datas = datas;
	}

	private RespMessage() {
		this.status = Status.OK.status;
	}

	private RespMessage(Integer status, String err, Object datas) {
		super();
		this.status = status;
		this.err = err;
		this.datas = datas;
	}

	public static RespMessage success() {
		return RespMessage.builder().setStatus(Status.OK).setErr(Message.COMMON_SUCCESS).build();
	}

	public static RespMessage success(String msg) {
		return RespMessage.builder().setStatus(Status.OK).setErr(msg).build();
	}

	public static RespMessage success(Object data) {
		return RespMessage.builder().setStatus(Status.OK).setErr(SpringUtils.getMessage(Message.COMMON_SUCCESS)).setDatas(data).build();
	}

	public static RespMessage success(Object datas, String msg, Object... args) {
		return RespMessage.builder().setStatus(Status.OK).setErr(msg, args).setDatas(datas).build();
	}

	public static RespMessage error(String msg, Object... args) {
		return RespMessage.builder().setStatus(Status.ERROR).setErr(msg, args).build();
	}

	public static RespMessage error(Status status, String msg, Object... args) {
		return RespMessage.builder().setStatus(status).setErr(msg, args).build();
	}

	public static RespMessage validError() {
		return VALID_ERROR;
	}

	public static RespMessage unknownError() {
		return UNKNOWN_ERROR;
	}

	/**
	 * 没有访问权限
	 * 
	 * @return
	 */
	public static RespMessage noPermissionError() {
		return NO_AUTH_ERROR;
	}

	public static RespMessageBuilder builder() {
		return new RespMessageBuilder();
	}

	public static class RespMessageBuilder {
		Status status;
		String err = "";
		Object datas;

		public RespMessage build() {
			Assert.notNull(status);
			return new RespMessage(status.status, err, datas);
		}

		public RespMessageBuilder success() {
			this.status = Status.OK;
			this.setErr(Message.COMMON_SUCCESS);
			return this;
		}

		public RespMessageBuilder error() {
			this.status = Status.ERROR;
			this.setErr(Message.COMMON_FAIL);
			return this;
		}

		public RespMessageBuilder validError() {
			this.status = Status.VALID_ERR;
			this.setErr(Message.FORM_VALID_FAIL);
			return this;
		}

		public RespMessageBuilder invalidTokenError() {
			this.status = Status.INVALID_TOKEN;
			setErr(Message.LOGIN_INVALID_TOKEN);
			return this;
		}

		public RespMessageBuilder nonPermissionError() {
			this.status = Status.NON_PERMISSION;
			setErr(Message.COMMON_NON_PERMISSION);
			return this;
		}

		public RespMessageBuilder unknownError() {
			this.status = Status.UNKNOWN_ERR;
			return this;
		}

		public RespMessageBuilder setStatus(Status status) {
			this.status = status;
			return this;
		}

		public RespMessageBuilder setErr(String err, Object... args) {
			this.err = SpringUtils.getMessage(err, args);
			return this;
		}

		public RespMessageBuilder setDatas(Object datas) {
			this.datas = datas;
			return this;
		}

	}

	private static final RespMessage VALID_ERROR = new RespMessage(Status.VALID_ERR.status, SpringUtils.getMessage(Message.FORM_VALID_FAIL), null);

	private static final RespMessage UNKNOWN_ERROR = new RespMessage(Status.UNKNOWN_ERR.status, SpringUtils.getMessage(Message.COMMON_UNKNOWN_ERROR), null);

	private static final RespMessage NO_AUTH_ERROR = new RespMessage(Status.NON_PERMISSION.status, SpringUtils.getMessage(Message.COMMON_NON_PERMISSION), null);

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}
}
